﻿package com.eve.eplus.main;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.CheckBox;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.Spinner;

import com.eve.eplus.R;
import com.eve.eplus.common.activity.BaseActivity;
import com.eve.eplus.common.app.Constants;
import com.eve.eplus.common.app.SettingConstants;
import com.eve.eplus.common.tools.LogTools;
import com.eve.eplus.common.view.ComToastTools;
import com.eve.eplus.main.logic.SettingUtility;

/**
 * 设置界面
 * 
 * @author Norsie
 * @create 12-11-29
 */
public class SetActivity extends BaseActivity
{

    private FrameLayout btnAccountSet, btnFeedbackSet,
            btnAboutSet, btnUpdateSet, btnHelpSet, btnShoSet;
    private CheckBox chkVoice, chkHello, chkCam;
    private Spinner spFont, spPic, spRef;
    private Context mContext = SetActivity.this;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_main);
        initView();
    }

    /**
     * 界面初始化
     */
    private void initView()
    {
        btnAccountSet = (FrameLayout) findViewById(R.id.id_set_account);
        btnFeedbackSet = (FrameLayout) findViewById(R.id.id_set_feedback);
        btnAboutSet = (FrameLayout) findViewById(R.id.id_set_about);
        btnUpdateSet = (FrameLayout) findViewById(R.id.id_set_update);
        btnHelpSet = (FrameLayout) findViewById(R.id.id_set_help);
        btnShoSet = (FrameLayout) findViewById(R.id.id_set_sho);
        chkVoice = (CheckBox) findViewById(R.id.id_set_voicechexkbox);
        chkHello = (CheckBox) findViewById(R.id.id_set_hellochexkbox);
        chkCam = (CheckBox) findViewById(R.id.id_set_camchexkbox);
        spFont = (Spinner) findViewById(R.id.spinner_font);
        spPic = (Spinner) findViewById(R.id.spinner_pic);
        spRef = (Spinner) findViewById(R.id.spinner_ref);

        btnAccountSet.setOnClickListener(onClickListener);
        btnFeedbackSet.setOnClickListener(onClickListener);
        btnAboutSet.setOnClickListener(onClickListener);
        btnUpdateSet.setOnClickListener(onClickListener);
        btnHelpSet.setOnClickListener(onClickListener);
        btnShoSet.setOnClickListener(onClickListener);
        spFont.setOnItemSelectedListener(onItemSelectedListener);
        spPic.setOnItemSelectedListener(onItemSelectedListener);
        spRef.setOnItemSelectedListener(onItemSelectedListener);

        /**  **/
        if (SettingUtility.getWelcomeSetting() == SettingConstants.WELCOME_FALSE)
            chkHello.setChecked(false);
        else if (SettingUtility.getWelcomeSetting() == SettingConstants.WELCOME_TRUE)
            chkHello.setChecked(true);
        /**  **/
        if (SettingUtility.getCameraSaveSetting() == SettingConstants.CAMERA_SAVE_FALSE)
            chkCam.setChecked(false);
        else if (SettingUtility.getCameraSaveSetting() == SettingConstants.CAMERA_SAVE_TRUE)
            chkCam.setChecked(true);
        /**  **/
        if (SettingUtility.getTextFontSizeSetting() == SettingConstants.TEXT_FONTSIZE_SMALL)
            spFont.setSelection(0, false);
        else if (SettingUtility.getTextFontSizeSetting() == SettingConstants.TEXT_FONTSIZE_NORMAL)
            spFont.setSelection(1, false);
        else if (SettingUtility.getTextFontSizeSetting() == SettingConstants.TEXT_FONTSIZE_BIG)
            spFont.setSelection(2, false);
        /**  **/
        if (SettingUtility.getImgLoadTypeSetting(false) == SettingConstants.IMG_LOADTYPE_NONE)
            spPic.setSelection(0, false);
        else if (SettingUtility.getImgLoadTypeSetting(false) == SettingConstants.IMG_LOADTYPE_THUMBNAIL)
            spPic.setSelection(1, false);
        else if (SettingUtility.getImgLoadTypeSetting(false) == SettingConstants.IMG_LOADTYPE_BMIDDLE)
            spPic.setSelection(2, false);
        else if (SettingUtility.getImgLoadTypeSetting(false) == SettingConstants.IMG_LOADTYPE_AUTO)
            spPic.setSelection(3, false);

        /**  **/
        if (SettingUtility.getAutoReflashSetting(false) == SettingConstants.AUTOREFLASH_FALSE)
            spRef.setSelection(0, false);
        else if (SettingUtility.getAutoReflashSetting(false) == SettingConstants.AUTOREFLASH_TRUE)
            spRef.setSelection(1, false);
        else if (SettingUtility.getAutoReflashSetting(false) == SettingConstants.AUTOREFLASH_AUTO)
            spRef.setSelection(2, false);
        
        
        /** **/
        chkVoice.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {
                
            }
        });
        chkHello.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {
                LogTools.info(buttonView.toString()+"is"+isChecked);
                if (!isChecked) {
                    SettingUtility.setWelcomeSetting(SettingConstants.WELCOME_FALSE);
                }
                else {
                    SettingUtility.setWelcomeSetting(SettingConstants.WELCOME_TRUE);
                }
                
            }
        });
        chkCam.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {
                if (!isChecked) {
                    SettingUtility.setCameraSaveSetting(SettingConstants.CAMERA_SAVE_FALSE);
                }
                else {
                    SettingUtility.setCameraSaveSetting(SettingConstants.CAMERA_SAVE_TRUE);
                }
                LogTools.info(buttonView.toString()+"is"+isChecked);
            }
        });
    }

    OnClickListener onClickListener = new OnClickListener() {

        @Override
        public void onClick(View v)
        {
            // TODO Auto-generated method stub
            switch (v.getId())
            {
                case R.id.id_set_account:
                    gotoActivity(mContext, AccountManagerActivity.class);
                    break;
                case R.id.id_set_about:
                    gotoActivity(mContext, AboutActivity.class);
                    break;
                case R.id.id_set_help:
                    Intent intent = new Intent(mContext,
                                               IntroduceActivity.class);
                    intent.putExtra(Constants.ACTIVITY_SOURCE,
                            Constants.ACTIVITY_SOURCE_SETACTIVITY);
                    gotoActivity(mContext, intent);
                    break;
                default:
                    break;
            }
        }
    };

    OnItemSelectedListener onItemSelectedListener = new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id)
        {

            switch (parent.getId())
            {
                case R.id.spinner_font:
                    switch (position)
                    {
                        case 0:
                            SettingUtility.setTextFontSizeSetting(SettingConstants.TEXT_FONTSIZE_SMALL);
                            break;
                        case 1:
                            SettingUtility.setTextFontSizeSetting(SettingConstants.TEXT_FONTSIZE_NORMAL);
                            break;
                        case 2:
                            SettingUtility.setTextFontSizeSetting(SettingConstants.TEXT_FONTSIZE_BIG);
                            break;

                        default:
                            break;
                    }
                    break;
                case R.id.spinner_pic:
                    switch (position)
                    {
                        case 0:
                            SettingUtility.setImgLoadTypeSetting(SettingConstants.IMG_LOADTYPE_NONE);
                            break;
                        case 1:
                            SettingUtility.setImgLoadTypeSetting(SettingConstants.IMG_LOADTYPE_THUMBNAIL);
                            break;
                        case 2:
                            SettingUtility.setImgLoadTypeSetting(SettingConstants.IMG_LOADTYPE_BMIDDLE);
                            break;
                        case 3:
                            SettingUtility.setImgLoadTypeSetting(SettingConstants.IMG_LOADTYPE_AUTO);
                            break;

                        default:
                            break;
                    }
                    break;
                case R.id.spinner_ref:
                    switch (position)
                    {
                        case 0:
                            SettingUtility.setAutoReflashSetting(SettingConstants.AUTOREFLASH_FALSE);
                            break;
                        case 1:
                            SettingUtility.setAutoReflashSetting(SettingConstants.AUTOREFLASH_TRUE);
                            break;
                        case 2:
                            SettingUtility.setAutoReflashSetting(SettingConstants.AUTOREFLASH_AUTO);
                            break;

                        default:
                            break;
                    }
                    break;

                default:
                    break;
            }

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent)
        {
            // TODO Auto-generated method stub

        }
    };
}
